// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CategoryCollection.cs" company="imbVeles" >
//
// Copyright (C) 2018 imbVeles
//
// This program is free software: you can redistribute it and/or modify
// it under the +terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/.
// </copyright>
// <summary>
// Project: imbSCI.Graph
// Author: Goran Grubic
// ------------------------------------------------------------------------------------------------------------------
// Project web site: http://blog.veles.rs
// GitHub: http://github.com/gorangrubic
// Mendeley profile: http://www.mendeley.com/profiles/goran-grubi2/
// ORCID ID: http://orcid.org/0000-0003-2673-9471
// Email: hardy@veles.rs
// </summary>
// ------------------------------------------------------------------------------------------------------------------
using imbSCI.Graph.DGML.core;
using System;
using System.Collections.Generic;
using System.Linq;

namespace imbSCI.Graph.DGML.collections
{
    public class CategoryCollection : List<Category>
    {
        public CategoryCollection()
        {
        }

        /// <summary>
        /// Gets the <see cref="Category"/> with the specified identifier.
        /// </summary>
        /// <value>
        /// The <see cref="Category"/>.
        /// </value>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public Category this[String id]
        {
            get
            {
                return this.FirstOrDefault(x => x.Id == id);
            }
        }

        public void AddUnique(Category cat)
        {
            if (!this.Any(x => (x.Id == cat.Id)))
            {
                Add(cat);
            }
            else
            {
            }
        }

        /// <summary>
        /// Adds the or get category.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="basedOn">The based on.</param>
        /// <returns></returns>
        public Category AddOrGetCategory(Int32 id, String label, String basedOn)
        {
            String sid = id.ToString();
            if (!this.Any(x => x.Id == sid))
            {
                Category cat = new Category(id, label, basedOn);
                AddUnique(cat);
                return cat;
            }
            return this[sid];
        }

        public Category AddOrGetCategory(String sid, String label, String basedOn)
        {
            if (!this.Any(x => x.Id == sid))
            {
                Category cat = new Category(label, basedOn);
                cat.Id = sid;
                AddUnique(cat);
                return cat;
            }
            return this[sid];
        }
    }
}